/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grest.controller;
import grest.view.VistaConfig;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import grest.model.Configu;
import grest.model.DataIO;
import grest.view.FormAdmin;
import grest.view.FormMozos;
import javax.swing.*;
import java.awt.*;
import java.io.*;



/**
 *
 * @author rodrigo
 */
public class CntrlrConfig implements ActionListener{
    
    private VistaConfig form = null;
    private static Boolean estado = false;
    private Configu c = null;
    
    
   
    public CntrlrConfig()
    {
        
    }
    
    public CntrlrConfig(VistaConfig form){
        this.form = form;
        
    }
    
    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==form.getControls("btnEstablecer")){    
            c = new Configu();
            c.setUser(((JTextField)form.getControls("txtUser")).getText());
            c.setPass(String.valueOf(((JPasswordField)form.getControls("txtPass")).getPassword()));
            c.setPath(((JTextField)form.getControls("txtBDPath")).getText());
            c.setDiasCancelReserv(Integer.parseInt(((JTextField)form.getControls("txtAnticipa")).getText()));
            c.setDiasMostrarReserv(Integer.parseInt(((JTextField)form.getControls("txtIntervalo")).getText()));
            try{
                FileOutputStream fileOut =
                new FileOutputStream("configuracion.ser");
                ObjectOutputStream out =
                                new ObjectOutputStream(fileOut);
                out.writeObject(c);
                out.close();
                fileOut.close();
                ((JButton)form.getControls("btnConectarse")).setEnabled(true);
            }catch(IOException i)
            {
              JOptionPane.showMessageDialog(this.form, "Se ha producido el siguiente error: " +i.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
            }  
        }
        else if (e.getSource()==form.getControls("btnConectarse"))
        {   
            try{
                FileInputStream fileIn =
                              new FileInputStream("configuracion.ser");
                ObjectInputStream in = new ObjectInputStream(fileIn);
                c = (Configu) in.readObject();
                in.close();
                fileIn.close();
                DataIO.setUser(c.getUser());
                DataIO.setUrl(c.getPath());
                DataIO.setPass(c.getPass());
                grest.GRest.setDiasMostrarReserv(c.getDiasMostrarReserv());
                grest.GRest.setDiasCancelReserv(c.getDiasCancelReserv());
                try{ 
                    DataIO.init();
                    if(grest.GRest.isUserMozo()){
                        FormMozos frmMozos = new FormMozos();
                    }else{
                        FormAdmin frmPrincipal = new FormAdmin();
                        grest.GRest.bajaReservas(grest.GRest.getDiasCancelReserv());
                    }
                    // 
                    form.getForm().dispose();
                }catch(java.lang.NullPointerException n)
                {
                    JOptionPane.showMessageDialog(null,"Error de conexión. Verifique sus datos","Error", JOptionPane.ERROR_MESSAGE);
                    grest.GRest.createFormConfig();
                    form.getForm().dispose();
                }
            }catch(IOException i)
            {
                JOptionPane.showMessageDialog(this.form, "Se ha producido el siguiente error: " +i.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
            }catch(ClassNotFoundException cs)
            {
               JOptionPane.showMessageDialog(this.form, "Se ha producido el siguiente error: " +cs.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
            
            } 
        }
    }
}